<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="Node.js
Node.js需要有一个解析执行环境，这个环境就是chrome 的 V8 引擎，node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。
在Node中执行相关的JS代码的两种">
    

    <!--Author-->
    
        <meta name="author" content="John Doe">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="Node.js"/>
    

    <!--Open Graph Description-->
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="Hexo"/>

    <!--Type page-->
    
        <meta property="og:type" content="article" />
    

    <!--Page Cover-->
    

        <meta name="twitter:card" content="summary" />
    

    <!-- Title -->
    
    <title>Node.js - Hexo</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet" />

    <!-- Google Analytics -->
    


    <!-- favicon -->
    
	
</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">逐风</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>Node.js</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                        
                            2017-06-13
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>Node.js</p>
<p>Node.js需要有一个解析执行环境，这个环境就是chrome 的 V8 引擎，node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。<br>Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。</p>
<p>在Node中执行相关的JS代码的两种方式<br>1直接在命令行中输入node，进入Node的REPL运行环境：<br>2将Node代码写入到一个js文件中，然后通过node <code>要执行的JS文件路径</code> 去运行Node代码<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">指令使用流程</div><div class="line">先使用npm init -y初始化一下包管理文件package.json，将来所有安装的包，都会记录到这个文件中</div><div class="line">使用npm install 包名 --save/--save-dev去安装包；其中，install可以简写成i;</div><div class="line">--save表示把包安装到部署依赖中（在开发和部署上线都需要使用的包）； </div><div class="line">--save-dev表示安装到开发依赖（只在项目开发阶段需要用到的包，比如gulp）；</div><div class="line">--save可以简写成-S；--save-dev可以简写成-D;</div><div class="line">删除包npm uninstall 包名 --save/--save-dev或者npm remove 包名 --save/--save-dev</div><div class="line">npm i gulp -g,其中，-g表示全局安装某些包，通过-g安装的包都在C:\Users\用户名\AppData\Roaming\npm下面</div><div class="line">全局安装的包可以通过 cmd 命令行在任何地方使用；</div><div class="line">发给别人别人下载，如果只需要安装项目运行的依赖，不用gulp之类的包，可以运行npm i --production</div></pre></td></tr></table></figure></p>
<p>使用fs模块来操作文件<br>fs.readFile</p>
<p>fs.writeFile 没有就创建，有就覆盖</p>
<p>fs.appendFile  没有就创建，有就追加<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">使用http模块创建基本web服务器</div><div class="line">//导入 http 核心模块</div><div class="line">var http = require(&apos;http&apos;);</div><div class="line">//调用 http.createServer 方法，创建服务器</div><div class="line">var server = http.createServer();</div><div class="line">//给创建的 server 服务器，绑定 request 事件处理函数</div><div class="line">//  request 事件处理函数，有两个参数，</div><div class="line">//    第一个是 request 请求对象，包含了和请求有关的数据，比如 url 地址， 请求 的query 查询字符串， 提交的form表单， cookie</div><div class="line">//    第二个参数是 respond响应对象，可以将服务器处理好的数据，响应给客户端去渲染；</div><div class="line">server.on(&apos;request&apos;, (req, res) =&gt; &#123;</div><div class="line">  console.log(&apos;OK&apos;)</div><div class="line">  // res.write(&apos;hello &apos;);</div><div class="line">  // res.write(&apos;world.&apos;);</div><div class="line">  // res.write(&apos;hello world.&apos;);</div><div class="line">  // 调用 res.end() 结束当前响应</div><div class="line">  // 这种方式，表示 向客户端发送内容，并立即结束响应；</div><div class="line">  // res.end(&apos;hello world.&apos;);</div><div class="line"></div><div class="line">  // res.writeHeader 第一个参数是状态码，第二个参数是响应头对象；</div><div class="line">  res.writeHeader(200, &#123; // text/plain 是普通文本</div><div class="line">    &apos;Content-Type&apos;: &apos;text/html; charset=utf-8&apos;  //text/css    image/png   </div><div class="line">  &#125;);</div><div class="line"></div><div class="line">  // res.end(&apos;啊啊啊啊&apos;);</div><div class="line">  res.end(&apos;&lt;h1&gt;啊啊啊啊啊&lt;/h1&gt;&apos;);</div><div class="line">&#125;)</div><div class="line"></div><div class="line">// 通过 server.listen 方法启动服务器，同时指定 端口号、IP地址(可选)，</div><div class="line">// 启动完成后的回调函数</div><div class="line">  server.listen(3000, &apos;127.0.0.1&apos;, () =&gt; &#123;</div><div class="line">  console.log(&apos;server running at http://127.0.0.1:3000&apos;)</div><div class="line">&#125;);</div></pre></td></tr></table></figure></p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr />

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2018 John Doe<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>